﻿using ExpressesClass.BestApi.Utils;
using ExpressesClass.Model;
using ExpressesClass.ZopApi.Base;
using ExpressesClass.ZopApi.billApplyNotify.request;
using ExpressesClass.ZopApi.billApplyNotify.response;
using ExpressesClass.ZopApi.billMarkNotify.request;
using ExpressesClass.ZopApi.billMarkNotify.response;
using ExpressesClass.ZopApi.billQueryNotify.request;
using ExpressesClass.ZopApi.billQueryNotify.response;
using ExpressesClass.ZopApi.billTraceNotify.request;
using ExpressesClass.ZopApi.billTraceNotify.response;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace ExpressesClass.IExpresses
{
    public class ZopExpress : IExpress
    {
        //中通快递参数
        private static string url = "http://japi.zto.cn";//对应的地址
        private static string companyId = "";//合作商编码
        private static string key = "";//合作商签名key
        private static string partner = "";//商家ID，又称电子面单账号、电子面单账户、客户ID
        private static string verify = "";//电子面单账号验证码，由合作网点提供
        private static string branchId = "";//网点编码，客户购买电子面单的网点
        //中通测试参数
        //private static string url = "http://58.40.16.120:9001";
        //private static string companyId = "";
        //private static string key = "";
        //private static string partner = "";
        //private static string verify = "";
        //private static string branchId = "";
        public Kddh GetMailNo(Kddh kddh)
        {
            ZopProperties properties = new ZopProperties(companyId, key);
            ZopClient client = new ZopClient(properties);
            billApplyNotifyReq request = new billApplyNotifyReq();
            request.url = url + request.appName;
            //寄件人信息
            billApplyNotifyReqDataSender sender = new billApplyNotifyReqDataSender();
            sender.name = kddh.sendMan;
            sender.mobile = kddh.sendManPhone;
            sender.phone = kddh.sendManPhone;
            sender.address = kddh.sendManAddress;
            sender.city = kddh.sendCity;
            sender.zipCode = kddh.sendPostcode;
            sender.company = kddh.sendCompany;
            //收件人信息
            billApplyNotifyReqDataReceiver receiver = new billApplyNotifyReqDataReceiver();
            receiver.name = kddh.receiveMan;
            receiver.mobile = kddh.receiveManPhone;
            receiver.phone = kddh.receiveManPhone;
            receiver.address = kddh.receiveManAddress;
            receiver.city = kddh.receiveCity;
            receiver.zipCode = kddh.receivePostcode;
            receiver.company = kddh.receiveCompany;
            //快件明细
            billApplyNotifyReqDataContent content = new billApplyNotifyReqDataContent();
            content.sender = sender;
            content.receiver = receiver;
            content.id = kddh.orderId;
            content.typeId = "1";
            content.branchId = branchId;
            content.remark = kddh.remark;
            content.quantity = kddh.itemCount;
            content.weight = double.Parse(kddh.itemWeight);
            //内容
            billApplyNotifyReqDataDetail reqdata = new billApplyNotifyReqDataDetail();
            reqdata.content = content;
            reqdata.partner = partner;
            reqdata.verify = verify;
            reqdata.datetime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

            request.addParam(request.paramName, JsonParser.objToJson(reqdata));

            billApplyNotifyResDataDetail response = JsonParser.jsonToObj<billApplyNotifyResDataDetail>(client.execute(request));

            if (response != null && response.result)
            {
                billMarkNotifyResDataContent billMarkNotifyResDataContent = GetMark(response.data.billCode, kddh);
                Kddh k = new Kddh();
                k.orderId = kddh.orderId;
                k.mailNo = response.data.billCode;
                k.markDestination = "";
                k.sortingCode = billMarkNotifyResDataContent.mark;
                k.pkgCode = billMarkNotifyResDataContent.bagAddr;
                k.billProvideSiteName = response.data.siteName;
                k.billProvideSiteCode = response.data.siteCode;
                k.sendMan = kddh.sendMan;
                k.sendManPhone = kddh.sendManPhone;
                k.sendManAddress = kddh.sendManAddress;
                k.sendPostcode = kddh.sendPostcode;
                k.sendProvince = kddh.sendProvince;
                k.sendCity = kddh.sendCity;
                k.sendCounty = kddh.sendCounty;
                k.sendCompany = kddh.sendCompany;
                k.receiveMan = kddh.receiveMan;
                k.receiveManPhone = kddh.receiveManPhone;
                k.receiveManAddress = kddh.receiveManAddress;
                k.receivePostcode = kddh.receivePostcode;
                k.receiveProvince = kddh.receiveProvince;
                k.receiveCity = kddh.receiveCity;
                k.receiveCounty = kddh.receiveCounty;
                k.receiveCompany = kddh.receiveCompany;
                k.shipName = kddh.shipName;
                k.shipVal = kddh.shipVal;
                k.itemName = kddh.itemName;
                k.itemWeight = kddh.itemWeight;
                k.itemCount = kddh.itemCount;
                k.remark = kddh.remark;
                k.bm = kddh.bm;
                k.wzh = kddh.wzh;
                k.insuranceValue = "0";
                k.kdCompanys = kddh.kdCompanys;
                k.scenarios = kddh.scenarios;
                k.printNum = kddh.printNum;
                k.payMethod = kddh.payMethod;
                k.isReturnNo = kddh.isReturnNo;
                return k;
            }
            else if (response != null && response.data == null)
            {
                MessageBox.Show(response.message, "提示");
            }
            else if (response != null && response.data != null)
            {
                MessageBox.Show(response.data.message, "提示");
            }
            else
            {
                MessageBox.Show("获取快递电子面单失败.", "提示");
            }
            return null;
        }
        public bool CancelMailNo(Kddh kddh)
        {
            MessageBox.Show("中通快递目前不支持取消订单.", "提示");
            return false;
        }
        public void GetUseNum()
        {
            ZopProperties properties = new ZopProperties(companyId, key);
            ZopClient client = new ZopClient(properties);
            billQueryNotifyReq request = new billQueryNotifyReq();
            request.url = url + request.appName;

            //内容
            billQueryNotifyReqDataDetail reqdata = new billQueryNotifyReqDataDetail();
            reqdata.partner = partner;
            reqdata.type = "";
            reqdata.LastNo = "";

            request.addParam(request.paramName1, JsonParser.objToJson(reqdata));
            request.addParam(request.paramName2, request.msgType);

            billQueryNotifyResDataDetail response = JsonParser.jsonToObj<billQueryNotifyResDataDetail>(client.execute(request));

            if (response != null && response.result)
            {
                MessageBox.Show("可用量: " + response.data.available, "提示");
            }
            else if (response != null)
            {
                MessageBox.Show(response.message, "提示");
            }
            else
            {
                MessageBox.Show("查询失败", "提示");
            }
        }
        public List<TraceLog> GetExpressMsg(Kddh kddh)
        {
            ZopProperties properties = new ZopProperties(companyId, key);
            ZopClient client = new ZopClient(properties);
            billTraceNotifyReq request = new billTraceNotifyReq();
            request.url = url + request.appName;

            //内容
            request.addParam(request.paramName1, JsonParser.objToJson(new List<string>() { kddh.mailNo }));
            request.addParam(request.paramName2, request.msgType);

            billTraceNotifyResDataDetail response = JsonParser.jsonToObj<billTraceNotifyResDataDetail>(client.execute(request));

            if (response != null && response.status)
            {
                List<TraceLog> log = new List<TraceLog>();

                foreach (billTraceNotifyResDataTrace trace in response.data[0].traces)
                {
                    TraceLog t = new TraceLog();
                    t.acceptAddress = trace.scanSite;
                    t.acceptTime = trace.scanDate;
                    t.scanType = trace.scanType;
                    t.remark = trace.desc;
                    log.Add(t);
                }
                return log;
            }
            else if (response != null)
            {
                MessageBox.Show(response.msg, "提示");
            }
            else
            {
                MessageBox.Show("查询失败", "提示");
            }
            return null;
        }
        private billMarkNotifyResDataContent GetMark(string billCode, Kddh kddh)
        {
            ZopProperties properties = new ZopProperties(companyId, key);
            ZopClient client = new ZopClient(properties);
            billMarkNotifyReq request = new billMarkNotifyReq();
            request.url = url + request.appName;
            //寄件人信息
            billMarkNotifyReqDataDetail data = new billMarkNotifyReqDataDetail();
            data.unionCode = billCode;
            data.send_province = kddh.sendProvince;
            data.send_city = kddh.sendCity;
            data.send_district = kddh.sendCounty;
            data.send_address = kddh.sendManAddress;
            data.receive_province = kddh.receiveProvince;
            data.receive_city = kddh.receiveCity;
            data.receive_district = kddh.receiveCounty;
            data.receive_address = kddh.receiveManAddress;

            request.addParam(request.paramName1, JsonParser.objToJson(data));
            request.addParam(request.paramName2, request.msgType);

            billMarkNotifyResDataDetail response = JsonParser.jsonToObj<billMarkNotifyResDataDetail>(client.execute(request));

            if (response != null && response.status)
            {
                return response.result;
            }
            else if (response != null)
            {
                MessageBox.Show("获取集包地大头笔:" + response.message, "提示");
            }
            else
            {
                MessageBox.Show("获取集包地大头笔失败.", "提示");
            }
            return new billMarkNotifyResDataContent() { bagAddr = "", mark = "" };
        }
    }
}
